using System;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Acms.SqlDal
{
    public class PostControlDal : Base
    {
        public PostControlDal() : base() { }

        public enum Fields
        {
            Id,
            PostId,
            PostVersionId,
            TemplateControlId,
            Name,
            TypeId,
            TypeName,
            AdminControlType,
            Value,
            Notes
        } 

        protected override void ReadRecord(System.Collections.Hashtable table, SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.PostId, reader, table);
            GetReaderValue<int>(Fields.PostVersionId, reader, table);
            GetReaderValue<int>(Fields.TemplateControlId, reader, table);
            GetReaderValue<string>(Fields.Name, reader, table);
            GetReaderValue<int>(Fields.TypeId, reader, table);
            GetReaderValue<string>(Fields.TypeName, reader, table);
            GetReaderValue<string>(Fields.AdminControlType, reader, table);
            GetReaderValue<System.Xml.XmlDocument>(Fields.Value, reader, table);
            GetReaderValue<string>(Fields.Notes, reader, table);
        }

        public Hashtable[] GetAllByPostVersionId(int postVersionId)
        {
            SqlCommand cmd = CreateCommand("GetAllByPostVersionId");
            AddInputParameter(cmd, "PostVersionId", postVersionId, SqlDbType.Int);
            return ExecuteGetCollection(cmd);
        }

        public Hashtable GetByPostVersionIdAndTemplateControlId(int postVersionId, int templateControlId)
        {
            SqlCommand cmd = CreateCommand("GetByPostVersionIdAndTemplateControlId");
            AddInputParameter(cmd, "PostVersionId", postVersionId, SqlDbType.Int);
            AddInputParameter(cmd, "TemplateControlId", templateControlId, SqlDbType.Int);
            return ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllBySiteIdAndTypeName(int siteId, string typeName)
        {
            SqlCommand cmd = CreateCommand("GetAllBySiteIdAndTypeName");
            AddInputParameter(cmd, "SiteId", siteId, SqlDbType.Int);
            AddInputParameter(cmd, "TypeName", typeName, SqlDbType.VarChar, 50);
            return ExecuteGetCollection(cmd);
        }

        public void Update(ref int id, ref int postId, int postVersionId, int templateControlId, System.Xml.XmlDocument value, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "PostVersionId", postVersionId, SqlDbType.Int);
            AddInputParameter(cmd, "TemplateControlId", templateControlId, SqlDbType.Int);
            AddInputParameter(cmd, "Value", value, SqlDbType.Xml);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "PostIdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);
            
            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            postId = GetOutputValue<int>(cmd, "PostIdOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");

            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);

            ExecuteCommand(cmd);
        }

    }
}
